subroutine check_distance( iprev, lover )
use prec
use cell, only: natomMol, atpos, attyp, attypMol, rotatedMol, box, rbox, DistMin
use wallinfo, only: WallPos, nWallAtom, WallRemain, LAddWalls
implicit none
   !----------------------------------------------------------------------------
   integer, intent(in)   :: iprev
   logical, intent(inout):: lover
   !----------------------------------------------------------------------------
   integer               :: i, II, ip, jp, pair
   real(q)               :: R(3), dist
   real(q), parameter    :: Rwmin = 4.D0
   !                        Min distance^2  C-C      C-H    H-H
   !----------------------------------------------------------------------------
   lover = .false.
outer:do II = 1, iprev
      ip = attyp(II)
      do i = 1, natomMol
         R = rotatedMol(:,i) - atpos(:, II)
         R = R - nint( R*rbox ) * box
         jp= attypMol(i)
         dist = R(1) * R(1) + R(2) * R(2) + R(3) * R(3)
         !
         lover = dist.lt.(DistMin(ip)*DistMin(jp))
         if ( lover ) exit outer
         !
      enddo
   enddo outer
   if ( LAddWalls.and.(.not.lover) ) then
wall: do II = 1, nWallAtom
         if ( WallRemain(II).eq.0 ) cycle
         do i = 1, natomMol
            R = rotatedMol(:,i) - WallPos(:,II)
            R(1:2) = R(1:2) - nint( R(1:2)*rbox(1:2) ) * box(1:2)
            dist   = R(1) * R(1) + R(2) * R(2) + R(3) * R(3)
            lover  = dist.lt.Rwmin
            if ( lover ) exit wall
         enddo
      enddo wall
   endif  
   !
return
end subroutine
